package apis

type Job func()

type Worker struct {
	JobQueue chan Job
}

func NewWorker() Worker {
	return Worker{JobQueue: make(chan Job)}
}

func (w Worker) Run() {
	go func() {
		for job := range w.JobQueue {
			job()
			//wg.Done()
		}
	}()
}

type Pool struct {
	JobQueue    chan Job
	WorkerQueue chan chan Job
}

func NewPool(maxWorkers int) *Pool {
	workerQueue := make(chan chan Job, maxWorkers)
	jobQueue := make(chan Job)
	return &Pool{JobQueue: jobQueue, WorkerQueue: workerQueue}
}

func (p *Pool) Run() {
	for i := 0; i < cap(p.WorkerQueue); i++ {
		worker := NewWorker()
		//worker.Run(&sync.WaitGroup{})
		worker.Run()
		p.WorkerQueue <- worker.JobQueue
	}

	go func() {
		for job := range p.JobQueue {
			workerJobQueue := <-p.WorkerQueue
			workerJobQueue <- job
		}
	}()
}
